package 知识点复习.异步回调.异步任务的合并执行;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiFunction;
import java.util.function.Supplier;

//使用thenCombine()计算(10+10)*(10+10)
public class ThenCombineDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                Integer firstStep = 10 + 10;
                System.out.println(Thread.currentThread().getName() + ":firstStep out is " + firstStep);
                return firstStep;
            }
        });
        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                Integer secondStep = 10 + 10;
                System.out.println(Thread.currentThread().getName() + ":secondStep out is " + secondStep);
                return secondStep;
            }
        });
        CompletableFuture<Integer> future3 = future1.thenCombine(future2, new BiFunction<Integer, Integer, Integer>() {
            @Override
            public Integer apply(Integer step1Out, Integer step2Out) {
                return step1Out * step2Out;
            }
        });
        Integer result = future3.get();
        System.out.println(Thread.currentThread().getName() + ":out is " + result);
    }
}
